
/*************************************************************************
* Copyright (c) 2008, ShenZhen Coship Electronic Ltd Co.
* All rights reserved.
* 
* File Name:		CDJpeg.cpp
* Summary:			implementation of the CCDJpeg class.
* 
* Current Version:	1.0
* Author(s):
/------------------------------------------------------------------------/
revision			author				reason					date
1.0					Ding Ning			Initialize Version		2008-6-18
/------------------------------------------------------------------------/
*************************************************************************/


#include "stdafx.h"

extern "C"
{
#include "../jpeg-6b/cdjpeg.h"
}

#include "decompress.h"
#include "cdjpeg2.h"

#ifdef USE_DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

CCDJpeg::CCDJpeg()
{
	m_srcImage.magic = &m_srcImage;
	m_srcImage.index = 0;
	m_srcImage.buf = (char *)malloc(1024*512);
	m_srcImage.bufLen= 1024*512;

	m_destImage.magic = &m_destImage;
	m_destImage.index = 0;
	m_destImage.buf = (char *)malloc(1024*1024*2);
	m_destImage.bufLen = 1024*1024*2;

	m_bReversal = TRUE;
	m_pInfo = NULL;

	m_cType = CODEC_JPEG;
}

CCDJpeg::~CCDJpeg()
{
	free(m_srcImage.buf);
	free(m_destImage.buf);
}


BOOL CCDJpeg::Init(const char* pbuf, int len)
{
	return TRUE;
}

void CCDJpeg::Destroy()
{
	//m_pInfo = NULL;
}

int CCDJpeg::GetWidth()
{
	if (m_pInfo)
		return m_pInfo->biWidth;
	return 0;
}

int CCDJpeg::GetHeight()
{
	if (m_pInfo)
		return m_pInfo->biHeight;
	return 0;
}

int CCDJpeg::GetBPP()
{
	return m_pInfo->biBitCount;
}

BOOL CCDJpeg::Decode(const char* pbuf, int len, int subLen)
{
	if (pbuf == NULL || len <= 0)
		return FALSE;
	
	memcpy(m_srcImage.buf, pbuf, len);
	m_srcImage.bufLen = len;
	m_srcImage.index = 0;
	m_destImage.index = 0;
	m_srcLength = len;
	// modify by dingning 2008-1-25
	BOOL result;
	try
	{
		result = decompress(&m_srcImage, &m_destImage);
	}
	catch (...)
	{
		return FALSE;
	}
	//end modify by dingning 2008-1-25 debug divide zero exception.

	m_destLength = m_destImage.bufLen;
	return result;
}

void CCDJpeg::InitPicInfo()
{
	m_pInfo = (LPBITMAPINFOHEADER) &(m_destImage.buf[14]);
}

char* CCDJpeg::GetPicBuff()
{
	return &(m_destImage.buf[54]);
}

char* CCDJpeg::GetJpgPicBuff()
{
	return &(m_srcImage.buf[0]);
}

int CCDJpeg::getDestLength()
{
	return m_destLength;
}

int CCDJpeg::getSrcLength()
{
	return m_srcLength;
}

LPBITMAPINFOHEADER CCDJpeg::getInfo()
{
	return m_pInfo;
}

